<?php
// 应用公共文件
use think\facade\Env;
//邮箱验证
function checkEmail($email){
    $result  = filter_var($email, FILTER_VALIDATE_EMAIL);
    return $result;
}

function createSn(){

}

function createToken($len=64, $all="all", $bf=''){
    $chars = md5(uniqid(mt_rand(), true));  
    // return ;
    $uuid = substr ( $chars, 0, 8 ) . '-'
            . substr ( $chars, 8, 4 ) . '-' 
            . substr ( $chars, 12, 4 ) . '-'
            . substr ( $chars, 16, 4 ) . '-'
            . substr ( $chars, 20, 12 );  
    if($bf){
        return $bf.$uuid;
    }else{
        return $uuid;  
    }
}

function get_chu($len = 6, $all = 'all'){
    if($all == 'all'){
        $str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
    }

    if($all == 'number'){
        $str = '012345678901234567890123456789012345678901234567890123456789';
    }
    
    if($all == 'char'){
        $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
    }

    if($all == 'low'){
        $str = "23456789abcdefghjkminpqrstuvwxyz";
    }
    
    
    $shuffled = str_shuffle($str);
    //1.获取字符串的长度
    //4.随机截取字符串，取其中的一部分字符串
    $rts = substr($shuffled, 0,$len);
    return $rts;
}

//获取菜单树结构
function getMenuTree($data, $field='parent_id', $id = 0, $lev = 0, $is_show=0,$key_to_id=1, $ln='child',$show_ln=0){
    $arr = [];
    if(!$data){
        return false;
    }
    foreach($data as $k=>$v){
        if(!$is_show){
            if($v['is_show']!=1){
                continue;
            }
        }
        if($v[$field] == $id){
            $v['lev'] = $lev;
            $v[$ln] = getMenuTree($data, $field, $v['id'], $lev+1,$is_show,$key_to_id,$ln);
            if(!$v[$ln] && !$show_ln){
                unset($v[$ln]);
            }else{
                $v[$ln.'_count'] = count($v[$ln]);
            }
            if($key_to_id){
                $arr[$v['id']] = $v;
            }else{
                $arr[] = $v;
            }
        }
    }
    return $arr;
}

//curl get
function curlGet($url='', $header=[]){
    $ch = curl_init();
    // print_r($header);
    
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    // curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    // curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    // curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    // curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    // curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    if($header){
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    }
    
    // print_r($ch);
    $temp = curl_exec($ch);
    // echo 'Error:' . curl_error($ch);
    // if (curl_errno($ch)) {
    //     echo 'Error:' . curl_error($ch);
    // }

    curl_close($ch);

    // var_dump($temp);
    return $temp;
}

//写入日志
function sys_log($str='', $file='log.txt'){
    //是否开启此日志，开启日志会占用磁盘空间
    $open_log = config('app.open_log');
    if($open_log==false){
        return false;
    }

    $fileDir = date('Y-m-d',time());
    $fileDir = Env::get('ROOT_PATH').'log/'.$fileDir.'/';
    //按小时拆分
    if(!is_dir($fileDir)){
        mk_dir($fileDir);
    }
    $fileDir = $fileDir.'/'.date('H',time()).'/';
    if(!is_dir($fileDir)){
        mk_dir($fileDir);
    }
    $ft = fopen($fileDir.$file, 'a');
    fwrite($ft,'执行时间:'.date('Y-m-d H:i:s',time()).' '.$str."\n");
    fclose($ft);
}

// 循环创建目录
function mk_dir( $dir, $mode = 0777 ){
    if( is_dir( $dir ) || @mkdir( $dir, $mode ) ) {
        return true;
    }
    if ( !mk_dir( dirname( $dir ), $mode ) ) {
        return false;
    }
    return @mkdir( $dir, $mode );
}